package com.astar.controller.systemSetup;


import com.astar.dto.result.DefaultResult;
import com.astar.dto.result.sys.AjaxResult;
import com.astar.entity.*;
import com.astar.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-01-17
 */
@RequestMapping("/roleManage")
@Controller
public class RoleController {



    @Autowired
    private IRoleService roleServiceImpl;

    @Autowired
    private IUserService userServiceImpl;


    @Autowired
    private IMenuService menuServiceImpl;

    @Autowired
    private IRoleMenuService roleMenuServiceImpl;

    @Autowired
    private IUserRoleService userRoleServiceImpl;

    @RequestMapping("/index")
    public Object toIndex(){
        return "roleManage/index";
    }

    @RequestMapping("/tableShow")
    @ResponseBody
    public Object tableShow(String roleName){
        List<Role> roles = roleServiceImpl.selectList(roleName);
        return roles;
    }

    @RequestMapping("/toAdd")
    public Object toAdd(Integer roleId, Model model){
        if(roleId == null){
            return "roleManage/add";
        }
        Role role = roleServiceImpl.getById(roleId);
        model.addAttribute("role",role);
        return "roleManage/update";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Role role){
        if(role.getRoleId()==null){
            QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.orderByDesc("role_sort");
            Role role1 = roleServiceImpl.list(roleQueryWrapper).get(0);
            role.setRoleSort(role1.getRoleSort()+1);
            role.setStatus("0");
            LocalDateTime localDateTime3 = LocalDateTime.now();
            role.setCreateTime(localDateTime3);
        }else{
            LocalDateTime localDateTime3 = LocalDateTime.now();
            role.setUpdateTime(localDateTime3);
        }
        roleServiceImpl.saveOrUpdate(role);
        return AjaxResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Integer roleId){
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        roleMenuServiceImpl.remove(queryWrapper);
        Role role = new Role();
        role.setRoleId(roleId);
        role.setStatus("1");
        roleServiceImpl.delete(role);
        return AjaxResult.success();
    }

    @RequestMapping("/view")
    public Object detailRoles(Integer roleId,Model model){
        ModelAndView modelAndView = new ModelAndView();
        Role role = roleServiceImpl.getById(roleId);
        model.addAttribute("role",role);
        return "roleManage/view";
    }

    @RequestMapping("/setPower")
    @ResponseBody
    public Object setPower(Integer id,Model model){
        Map<String, Object> map = new HashMap<>();
        List<CustomMenu> treeList = roleServiceImpl.selectAllTree();
        List<RoleMenu> roleMenu = roleMenuServiceImpl.selectTreeCheck(id);
        map.put("treeList", treeList);
        map.put("roleRessour", roleMenu);
        return map;
    }

    @RequestMapping("/tree")
    public Object tree(Integer id, Model model) {

        model.addAttribute("ids", id);
        return "roleManage/setPower";
    }

    @RequestMapping("/addMenu")
    @ResponseBody
    public Object addMenu(String ids, Integer id) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(id);
        String[] split = ids.split(",");
        int i = 0;
        int g = roleMenuServiceImpl.deleteForRoleId(roleMenu);
            for (String ss : split) {
                RoleMenu roleMenu1 = new RoleMenu();
                roleMenu1.setMenuId(Integer.parseInt(ss));
                roleMenu1.setRoleId(id);
                i = roleMenuServiceImpl.insertSelective(roleMenu1);
            }
        return DefaultResult.buildDataResult(i);
    }

    @RequestMapping("/showUser")
    public Object showUser(Integer id, Model model) {
        List<CustomUser>  userList = userServiceImpl.selectAll(id);
        Role role = userServiceImpl.selectAllForId(id);
        model.addAttribute("userList",userList);
        model.addAttribute("role",role);
        model.addAttribute("id",id);

        return "roleManage/showUser";
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Object deleteById(Integer uid,Integer rid){
        UserRole userRole = new UserRole();
        userRole.setUserId(uid);
        userRole.setRoleId(rid);
        int g = userRoleServiceImpl.deleteRole(userRole);
        return DefaultResult.buildDataResult(g);
    }

    @RequestMapping("/userTree")
    public Object userTree(Integer id,Model model){
        List<CustomUser>  userList = userServiceImpl.selectAll(id);
        model.addAttribute("ssoUserList",userList);
        model.addAttribute("id",id);
        return "roleManage/userTree";
    }

    @RequestMapping("/deptTree")
    @ResponseBody
    public Object showUserTree(){
        List<Dept> ssoDepartmentList = userServiceImpl.selectDepTree();
        return ssoDepartmentList;
    }

    @RequestMapping("/toSelected")
    @ResponseBody
    public Object selected(){
        List<User> ssoUserList = userServiceImpl.select();
        return ssoUserList;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Object addUser(String userId,String roleId){
        String[] array = userId.split(",");
        int rid = Integer.parseInt(roleId);
        int g = 0;
        UserRole userRole = new UserRole();
        userRole.setRoleId(rid);
//        int g = ssoRoleService.deleteForRid(ssoUserRole);
        for (int i = 0;i<array.length;i++){
            UserRole userRoles = new UserRole();
            int uid = Integer.parseInt(array[i]);
            userRoles.setRoleId(rid);
            userRoles.setUserId(uid);
            g = userRoleServiceImpl.insertUserRole(userRoles);
        }
        return g;
    }






}
